import './interfaces';

function del<T extends BaseTable>(tname: string, data: T[]) {
	return data.map((it) => {
		return {
			criteria: it,
			opflag: 2,
			tname
		};
	}) as ArgJsonDelete[];
}

function do_del(systemid: string, argjson: ArgJsonDelete[]) {
	const msg = {
		argjson,
		spaceid,
		systemid
	};
	return send_msg('dbacc.save.intranet', msg) as Promise<{ code: number; result: any; }>;
}

interface Table1 extends BaseTable {
	field1: string;
}

interface Table2 extends BaseTable {
	field2: string;
}

interface Message {
	tb1: Table1[];
	tb2: Table2;	// just delete 1 record
}

async function atom(msg: Message, headers: IHeaders) {
	const argjson1 = del('@tablename1', msg.tb1);
	const argjson2 = del('@tablename2', [msg.tb2]);
	const ret = await do_del('systemid', argjson1.concat(argjson2));
	if (ret.code === 1) {
		return 'success';
	} else {
		return 'fail';
	}
}
